package com.ktjy.ktmall.dao.impl;

import com.ktjy.ktmall.dao.BaseDao;
import com.ktjy.ktmall.dao.CategoryItemDao;
import com.ktjy.ktmall.entity.CategoryItem;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 商品分类Dao接口实现类
 * @version v1.0
 * @Author 郴州科泰
 * @Create 2024-07-18 - 18:57
 */
public class CategoryItemDaoImpl extends BaseDao implements CategoryItemDao {

    /**
     * 查询前10个一级商品分类信息(用于在小程序首页处显示)
     */
    @Override
    public List<CategoryItem> getOneLevelCategory() {
        List<CategoryItem> list = new ArrayList<>();
        String sql = "select * from t_goods_category where category_level = 1 and `enabled` = 0 limit 10";
        rs = super.executeQuery(sql);
        try {
            while (rs.next()) {
                // 只需要读取到分类id、分类名称、分类图标
                CategoryItem c = new CategoryItem(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getString(4),rs.getInt(5),rs.getString(6));
                list.add(c);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }


    /**
     * 查询所有的商品分类列表
     * @return 返回查询到的商品分类列表
     */
    @Override
    public List<CategoryItem> findAllCategoryList() {
        List<CategoryItem> list = new ArrayList<>();
        String sql = "select * from t_goods_category where `enabled` = 0";
        rs = super.executeQuery(sql);
        try {
            while (rs.next()) {
                // 只需要读取到分类id、分类名称、分类图标
                CategoryItem c = new CategoryItem(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getString(4),rs.getInt(5),rs.getString(6));
                list.add(c);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return list;
    }

    /**
     * 根据类别id查询类别详情信息
     * @param id 类别id
     * @return 返回查询到的类别详情信息
     */
    @Override
    public CategoryItem findById(int id) {
        CategoryItem c = null;
        String sql = "select * from t_goods_category where `enabled` = 0 AND `id` =?";
        rs = super.executeQuery(sql, id);
        try {
            while (rs.next()) {
                // 只需要读取到分类id、分类名称、分类图标
                c = new CategoryItem(rs.getInt(1),rs.getInt(2),rs.getInt(3),rs.getString(4),rs.getInt(5),rs.getString(6));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            super.closeAll(rs, pstmt, conn);
        }
        return c;
    }
}
